﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Edge.Business.Domain.Application;
using Edge.Common.ComponentModel;
using Edge.Common.Services;
using Edge.Common.Utils;
using Shop.Business.Domain.Catalog;
using Shop.Business.Domain.Customers;
using Shop.Business.Domain.Orders;

namespace Shop.Web.Controls
{
    public partial class Draft : System.Web.UI.UserControl
    {
        private Customer customer;

        public List<OrderItem> OrderItems
        {
            get { return (List<OrderItem>)(Session["Order"] ?? null); }
            set { Session["Order"] = value; }
        }

        

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            ListDictionary sectionOrderMap = new ListDictionary();
            ListDictionary sectionPreOrderMap = new ListDictionary();
            if (Locator.GetService<IContextObjects>().Get<CustomerHolder>() != null)
            {
                customer = Locator.GetService<IContextObjects>().Get<CustomerHolder>().Object;
                Order order = Locator.GetService<IOrderManager>().GetOrderWithStatus(this.customer,OrderStatus.Draft);
                if (order.Items != null)
                foreach (OrderItem item in order.Items)
                {
                    string key = item.Product.Group.Category.Name;
                    if (item.Product.Qty > 0)
                    {
                        if (sectionOrderMap[key] == null)
                        {
                            sectionOrderMap[key] = new ArrayList();
                        }
                        ((IList) sectionOrderMap[key]).Add(item);
                    }
                    else
                    {
                        if (sectionPreOrderMap[key] == null)
                        {
                            sectionPreOrderMap[key] = new ArrayList();
                        }
                        ((IList)sectionPreOrderMap[key]).Add(item);
                    }
                }
            }
            else if (OrderItems != null && OrderItems.Count > 0)
            {
                
                foreach (OrderItem item in OrderItems)
                {
                    string key = item.Product.Group.Category.Name;
                    if (sectionOrderMap[key] == null)
                    {
                        sectionOrderMap[key] = new ArrayList();
                    }
                    ((IList)sectionOrderMap[key]).Add(item);
                }
               
            }
            if (sectionOrderMap.Count > 0)
            {
                rprMain.DataSource = sectionOrderMap;
                rprMain.DataBind();
                phOrders.Visible = true;
            }
            else
            {
                phOrders.Visible = false;
            }
            if (sectionPreOrderMap.Count > 0)
            {
                rprPreorder.DataSource = sectionPreOrderMap;
                rprPreorder.DataBind();
                phPreorders.Visible = true;
            }
            else
            {
                phPreorders.Visible = false;
            }
            phNoItems.Visible = sectionOrderMap.Count < 1 && sectionPreOrderMap.Count < 1;
            base.OnPreRender(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            ListDictionary sectionOrderMap = new ListDictionary();
            ListDictionary sectionPreOrderMap = new ListDictionary();
            if (Locator.GetService<IContextObjects>().Get<CustomerHolder>() != null)
            {
                customer = Locator.GetService<IContextObjects>().Get<CustomerHolder>().Object;
                Order order = Locator.GetService<IOrderManager>().GetOrderWithStatus(this.customer, OrderStatus.Draft);
                if (order.Items != null)
                    foreach (OrderItem item in order.Items)
                    {
                        string key = item.Product.Group.Category.Name;
                        if (item.Product.Qty > 0)
                        {
                            if (sectionOrderMap[key] == null)
                            {
                                sectionOrderMap[key] = new ArrayList();
                            }
                            ((IList)sectionOrderMap[key]).Add(item);
                        }
                        else
                        {
                            if (sectionPreOrderMap[key] == null)
                            {
                                sectionPreOrderMap[key] = new ArrayList();
                            }
                            ((IList)sectionPreOrderMap[key]).Add(item);
                        }
                    }
            }
            else if (OrderItems != null && OrderItems.Count > 0)
            {

                foreach (OrderItem item in OrderItems)
                {
                    string key = item.Product.Group.Category.Name;
                    if (sectionOrderMap[key] == null)
                    {
                        sectionOrderMap[key] = new ArrayList();
                    }
                    ((IList)sectionOrderMap[key]).Add(item);
                }

            }
            if (sectionOrderMap.Count > 0)
            {
                rprMain.DataSource = sectionOrderMap;
                rprMain.DataBind();
                phOrders.Visible = true;
            }
            else
            {
                phOrders.Visible = false;
            }
            if (sectionPreOrderMap.Count > 0)
            {
                rprPreorder.DataSource = sectionPreOrderMap;
                rprPreorder.DataBind();
                phPreorders.Visible = true;
            }
            else
            {
                phPreorders.Visible = false;
            }
            phNoItems.Visible = sectionOrderMap.Count < 1 && sectionPreOrderMap.Count < 1;
        }

        protected void btnOrder_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/OrderWizard.aspx");
        }

        protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            HtmlInputButton btnDelItem = (HtmlInputButton)e.Item.FindControl("btnDelItem");
            OrderItem item = (OrderItem)e.Item.DataItem;
            if (btnDelItem != null)
            {
                btnDelItem.Attributes["onclick"] = "Delete(" + (customer != null ? item.Id : item.Product.Id) + ")";
            }
        }
    }
}